package com.sensePlusVR.activity.mine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.xutils.x;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sensePlusVR.adapter.NotifiSettingAdapter;
import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.bean.FeedbackListRespDto;
import com.sensePlusVR.bean.FeedbackReplyItem;
import com.sensePlusVR.bean.FeedbackReqDto;
import com.sensePlusVR.main.R;
import com.sensePlusVR.manager.DataCenter;
import com.sensePlusVR.manager.EventListener;
import com.sensePlusVR.manager.FeedbackManager;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.MyLog;
/**
 * 
 * @author: Loren
 * @create: 2016-8-10下午2:44:13
 * @desc: 通知栏界面
 *
 */
@ContentView(R.layout.activity_settting_notification)
public class NotifiSettingActivity extends BaseActivity{
	private static final String TAG = NotifiSettingActivity.class
			.getSimpleName();
	private String KEY = this.getClass().getSimpleName();
	private List<FeedbackListRespDto> notifiList;
	private NotifiSettingAdapter adapter;
	@ViewInject(R.id.nitify_recylerview)
	private RecyclerView nitify_recylerview;
	@ViewInject(R.id.title_bar_title_tv)
	private TextView title_bar_title_tv;
	
	@ViewInject(R.id.nodata_relative)
	private RelativeLayout nodata_relative;
	
	private DataCenter dataCenter;
	
	@SuppressWarnings("unchecked")
	EventListener listener = new EventListener(){
		@Override
		public void Notify(int event, Object data, Object Params) {
			if (Params == null || data == null)
				return;
			MyLog.d(TAG, "event = "+event);
			if (event == FeedbackManager.EventFeedbackList){
				List<FeedbackListRespDto> feedbackListRespDtoList = (List<FeedbackListRespDto>) data;
				notifiList.clear();
				MyLog.d(TAG, "listener data size : " + feedbackListRespDtoList.size());
				for(FeedbackListRespDto feedbackListRespDto : feedbackListRespDtoList){
					if(feedbackListRespDto != null){
						List<FeedbackReplyItem> feedbackReplyItems = feedbackListRespDto.getFeedbackReplyList();
						if(feedbackReplyItems == null || (feedbackReplyItems != null && feedbackReplyItems.size() > 0)){
//							FeedbackReplyItem feedbackReplyItem = feedbackReplyItems.get(0);
//							if(feedbackReplyItem != null){
//								feedbackReplyItem.getCreateTime();
//							}
							notifiList.add(feedbackListRespDto);
						}
					}
				}
				if(notifiList.size() > 0){
					if(nodata_relative != null){
						nodata_relative.setVisibility(View.GONE);
					}
					Collections.reverse(notifiList);
					if(adapter != null){
						adapter.refreshData(notifiList);
					}

				}else{
					if(nodata_relative != null){
						nodata_relative.setVisibility(View.VISIBLE);
					}
				}
			}
		}
		
	};

	@Override
	public void setMyContentView() {
		x.view().inject(this);
	}

	@Override
	public void beforeView() {
		super.beforeView();
		dataCenter = DataCenter.getInstans();
		AppManager.getInstance().addActivity(this);
		title_bar_title_tv.setText(R.string.system_notification);
		notifiList = new ArrayList<FeedbackListRespDto>();
		//获取Feedback信息
		dataCenter.getFeedbackList(new FeedbackReqDto(0, 100));
		adapter = new NotifiSettingAdapter(NotifiSettingActivity.this,notifiList);
	}

	@Override
	public void initView() {
		super.initView();
		LinearLayoutManager layoutManager = new LinearLayoutManager(NotifiSettingActivity.this);
		nitify_recylerview.setLayoutManager(layoutManager);
		nitify_recylerview.setAdapter(adapter);
		
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		dataCenter.addFeedbackEventListener(listener, this, KEY);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		dataCenter.delFeedbackEventListener(KEY);
	}
	
	@Event(value = {R.id.title_bar_left_ib})
	private void setViewOnClick(View view){
		switch (view.getId()) {
		case R.id.title_bar_left_ib:
			AppManager.getInstance().finishActivity(this);
			break;
		}
	}
}
